package com.hotel;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.forever.common.FileManager;
import com.forever.common.MyUtil;
import com.forever.common.dao.CommonDAO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class HotelAction extends ActionSupport implements Preparable,ModelDriven<HotelDTO>{

	private static final long serialVersionUID = 1L;
	
	private HotelDTO dto;
	
	@Resource(name="dao")
	private CommonDAO dao;
	
	@Resource(name="myUtil")
	private MyUtil myUtil;
	
	@Resource(name="fileManager")
	private FileManager fileManager;
	
	@Override
	public HotelDTO getModel() {
		return dto;
	}

	@Override
	public void prepare() throws Exception {

		dto = new HotelDTO();		
		
	}
	
	public String save() throws Exception{
		
		if(dto == null || dto.getMode() == null || dto.getMode().equals(""))
			return INPUT;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		String root = session.getServletContext().getRealPath("/");
		String savePath = root + File.separator + "pds" + File.separator + "image";
		String newFileName = fileManager.doFileUpload(dto.getUpload(), dto.getUploadFileName(), savePath);
		
		if(newFileName != null){
			
			int maxFileNum = dao.getIntValue("hotel.fileMaxNum");
			dto.setBoardNum(maxFileNum+1);
			dto.setOriginalFileName(dto.getUploadFileName());
			dto.setSaveFileName(newFileName);			
			
			dao.insertData("hotel.insertData", dto);
		}
		
		return SUCCESS;
		
	}
	
	public String list() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String cp = request.getContextPath();
		
		int numPerPage = 4;
		int currentPage = 1;
		int totalPage = 0;
		int totalDataCount = 0;
		String urlImageFile = cp + "/pds/image"; 
		
		/*if(dto.getPageNum() != null && !(dto.getPageNum().equals("")))
			currentPage = Integer.parseInt(dto.getPageNum());*/
		
		totalDataCount = dao.getIntValue("hotel.dataCount");
		
		if(totalDataCount != 0)
			totalPage = myUtil.getPageCount(numPerPage, totalDataCount);
		
		if(currentPage > totalPage)
			currentPage = totalPage;
		
		Map<String, Object> hMap = new HashMap<String, Object>();
		int start = (currentPage - 1) * numPerPage + 1;
		int end = currentPage * numPerPage;
		
		hMap.put("start", start);
		hMap.put("end", end);
		
		List<Object> lists = (List<Object>)dao.getListData("hotel.listData", hMap);
		
		int listNum, n = 0;
		String str;
		ListIterator<Object> it = lists.listIterator();
		
		while(it.hasNext()){
			
			HotelDTO dto = (HotelDTO)it.next();
			listNum = totalDataCount - (start + n - 1);
			dto.setListNum(listNum);
			n++;
			
			/*str = cp + "/imageTest/download.action?fileNum=" + dto.getBoardNum();
			dto.setUrlFile(str);*/
			
		}
		
		String urlList = cp + "/hotel/list.action";
		request.setAttribute("pageNum", currentPage);
		request.setAttribute("totalDataCount", Integer.toString(totalDataCount));
		request.setAttribute("totalPage", Integer.toString(totalPage));
		request.setAttribute("pageIndexList", myUtil.pageIndexList(currentPage, totalPage, urlList));	
		request.setAttribute("urlImageFile", urlImageFile);
		
		return SUCCESS;
		
	}
/*
	public String delete() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		String root = session.getServletContext().getRealPath("/");
		String savePath = root + File.separator + "pds" + File.separator + "image";
		
		int fileNum = Integer.parseInt(request.getParameter("fileNum"));
		HotelDTO file = (HotelDTO)dao.getReadData("hotel.fileName", fileNum);
		fileManager.doFileDelete(file.getSaveFileName(), savePath);
		
		dao.deleteData("hotel.deleteData", fileNum);
		
		return SUCCESS;
	}*/
	
}
